package org.zfes.snowier.zbss.xzy.router.openserver;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.zfes.snowier.api.SnowierApisUtil;
import org.zfes.snowier.bss.biz.model.SysDeviceSecret;
import org.zfes.snowier.common.websocket.WebsocketMsgHandler;
import org.zfes.snowier.core.data.JSONMSG;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.core.util.encypt.AESUtil;
import org.zfes.snowier.web.controller.BaseController;
import org.zfes.snowier.xyz.live.ILiveRequestManager;
import org.zfes.snowier.xyz.live.IVideoStatusManager;
import org.zfes.snowier.zbss.xzy.vedio.LiveParamDto;

import com.alibaba.fastjson.JSON;

import io.swagger.annotations.ApiOperation;

@Controller 
@RequestMapping("/api/open/servive/live")
public class LiveAuthcController implements BaseController {

	@Autowired IVideoStatusManager videoStatusManager;

	@Autowired ILiveRequestManager liveRequestManager;
	
	@Autowired WebsocketMsgHandler websocketMsgHandler;
	
	  @ApiOperation(value = "流媒体服务器认证接口")
      @RequestMapping(value="/authc", method = {RequestMethod.POST,RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
      public  @ResponseBody Object authc(String deviceNum,String endata){
		  
		System.out.println("--LiveAuthcController-authc--deviceNum:"+deviceNum+"||endata:"+endata);
		  
		SysDeviceSecret secret = SnowierApisUtil.getSnowierApi().loadByDeviceSecret(deviceNum).orElseThrow(()->ZAlert.newSLE("未初始化秘钥"));
		
		String params = AESUtil.decrypt(endata, secret.getSymPubKey(),true);
		
		LiveParamDto liveParam = JSON.parseObject(params,LiveParamDto.class);
		
		System.out.println("--LiveAuthcController-authc--liveParam:"+liveParam);
		
//		boolean isChannelOnLive = videoStatusManager.isChannelOnLive(liveParam.getDeviceNum(), liveParam.getChannelId());
//		if(!isChannelOnLive) {
//			videoStatusManager.liveChannelStart(deviceNum, liveParam.getChannelId(),liveParam.getLiveId());
//		}
		
		boolean haveLiveRequest =  liveRequestManager.isChannelHaveRequest( liveParam.getDeviceNum(),  liveParam.getChannelId());
		if(haveLiveRequest) {
			videoStatusManager.liveChannelStart(deviceNum, liveParam.getChannelId(),liveParam.getLiveId());
		}
		
		
    	return ajaxDoneSuccess("ok").put("result", true);
      }
	  
	  @ApiOperation(value = "流媒体服务器--终端可以播放响应")
      @RequestMapping(value="/onStartLive", method = {RequestMethod.POST,RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
      public  @ResponseBody Object onStartLive(String deviceNum,String chnId,String endata){
		  System.out.println("--LiveAuthcController-onStartLive--deviceNum:"+deviceNum+"||chnId:"+chnId);
		  ZAssert.notNull(chnId, "chnId不能为空");
		  ZAssert.hasText(deviceNum, "设备编号不能为空");
		
		  SysDeviceSecret secret = SnowierApisUtil.getSnowierApi().loadByDeviceSecret(deviceNum).orElseThrow(()->ZAlert.newSLE("未初始化秘钥"));
			
		  String params = AESUtil.decrypt(endata, secret.getSymPubKey(),true);
			
		   LiveParamDto liveParam = JSON.parseObject(params,LiveParamDto.class);
			
		  System.out.println("--onStartLive---liveParam:"+liveParam);
			
		  videoStatusManager.liveChannelStart(deviceNum, liveParam.getChannelId(), liveParam.getLiveId());
		  
		  websocketMsgHandler.sendToUser("1",new JSONMSG(200)
				    .setWsfun("onStartLive")
	    			.put("channelId", chnId)
	    			.put("deviceNum", deviceNum));
		  
		  
    	return ajaxDoneSuccess("ok").put("result", true);
      }
	  
	  @ApiOperation(value = "流媒体服务器--终端停止播放响应")
      @RequestMapping(value="/onStopLive", method = {RequestMethod.POST,RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_VALUE)
      public  @ResponseBody Object onStopLive(String deviceNum,String chnId){
		  System.out.println("--LiveAuthcController-onStopLive--deviceNum:"+deviceNum+"||chnId:"+chnId);
		  
		  videoStatusManager.liveChannelStop(deviceNum, chnId);
		
    	return ajaxDoneSuccess("ok").put("result", true);
      }
}
